package com.wudizaba.qt.quartz.job;

import com.wudizaba.qt.po.ContentText;
import com.wudizaba.qt.po.QQnumber;
import com.wudizaba.qt.po.Student;
import com.wudizaba.qt.po.Title;
import com.wudizaba.qt.service.IStudentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import java.time.LocalDateTime;


/**
 * Created by wanggenshen
 * Date: on 2018/7/7 16:28.
 * Description: 打印任意内容
 */
@Slf4j
@RequiredArgsConstructor
//@DisallowConcurrentExecution
public class ScheduledTaskJob implements Job {

    private final IStudentService studentService;
    private final JavaMailSender  javaMailSender;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        log.info("当前时间：{}", LocalDateTime.now());
        Student byId = studentService.getById("01");
        log.info("数据：{}",byId.toString());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("任务执行完毕,准备发送邮件");
        //发送邮件
        String s = sendMail(QQnumber.FORM_MY_QQ_NUM,QQnumber.TO_QQ_NUM4,QQnumber.COM,Title.TITLE01,ContentText.TEXT_FOR_0);
        log.info("邮件发送结果：{}",s);
        log.info("邮件发送标题：{}",Title.TITLE01);
        log.info("邮件发送内容：{}",ContentText.TEXT_FOR_0);
        try {
            Thread.sleep(3000);
            log.info("邮件发送结束");
        } catch (InterruptedException e) {
            log.error("线程睡眠失败");
            e.printStackTrace();
        }
    }


    // 发送邮件,参数(发邮件qq号，接收邮件qq号，后缀，邮件标题，邮件信息)
    public String sendMail(String qq, String toqq, String com, String messageTitle, String messageText) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(qq + com);
        simpleMailMessage.setTo(toqq + com);
        simpleMailMessage.setSubject(messageTitle);
        simpleMailMessage.setText(messageText);
        try {
            javaMailSender.send(simpleMailMessage);
        } catch (Exception e) {
            e.printStackTrace();
            return "发送失败";
        }
        return "发送成功";
    }

}



